/*
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 *
 */

import * as React from "react";
import { connect } from "react-redux";
import { Form, Input, Button, Select } from "antd";

import { FormComponentProps } from "antd/lib/form";
import TextArea from "antd/lib/input/TextArea";
import { Dispatch, IRootState } from "src/store";

const mapStateToProps = ({ product, loading }: IRootState) => ({
  product,
  loading: loading.effects.product.getProductList as any
});
const mapDispatchToProps = ({ product }: Dispatch) => ({
  saveProduct: (payload: any) => product.saveProduct(payload)
});

type connectedProps = ReturnType<typeof mapStateToProps> &
  ReturnType<typeof mapDispatchToProps>;

interface IState {
  edit: boolean;
}

interface IProps extends connectedProps {
  edit: boolean;
  userData: any;
}

class ProductAdd extends React.Component<IProps & FormComponentProps, IState> {
  public state = {
    edit: false
  };

  public handleOk = (e: any) => {
    const { saveProduct } = this.props;
    e.preventDefault();
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        saveProduct(values);
      }
    });
  };

  public render() {
    const { getFieldDecorator } = this.props.form;
    return (
      <div style={{ paddingTop: "20px" }}>
        <Form>
          <Form.Item
            label={"产品名称"}
            labelCol={{ span: 8 }}
            wrapperCol={{ span: 8 }}
          >
            {getFieldDecorator("name", {
              initialValue: "",
              rules: [
                {
                  required: true,
                  message: "请填写产品名称"
                }
              ]
            })(<Input disabled={this.state.edit} />)}
          </Form.Item>
          <Form.Item
            label={"状态"}
            labelCol={{ span: 8 }}
            wrapperCol={{ span: 8 }}
          >
            {getFieldDecorator("status", {
              initialValue: "0",
              rules: [
                {
                  required: true,
                  message: "请选择状态"
                }
              ]
            })(
              <Select>
                <Select.Option value="0">未启用</Select.Option>
                <Select.Option value="1">启用</Select.Option>
              </Select>
            )}
          </Form.Item>
          <Form.Item
            label={"产品描述"}
            labelCol={{ span: 8 }}
            wrapperCol={{ span: 8 }}
          >
            {getFieldDecorator("description", {
              initialValue: "",
              rules: [
                {
                  required: true,
                  message: "请填写产品描述"
                }
              ]
            })(<TextArea rows={10} cols={10} />)}
          </Form.Item>
        </Form>

        <div style={{ textAlign: "center" }}>
          <Button
            type="primary"
            style={{ marginRight: "20px", marginBottom: "20px" }}
            onClick={this.handleOk}
          >
            保存
          </Button>
          {/* <Button type="default">关闭</Button> */}
        </div>
      </div>
    );
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Form.create<IProps & FormComponentProps>({})(ProductAdd));
